package group.receiver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class MessageReceiver {
    private InputStream inputStream;
    private BufferedReader reader;

    public MessageReceiver(Socket socket) {
        try {
            this.inputStream = socket.getInputStream();
            this.reader = new BufferedReader(new InputStreamReader(this.inputStream));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String receiveMessage() throws IOException {
        if (reader != null){
            StringBuilder sb = new StringBuilder();
            String text;
            while ((text = reader.readLine()) != null) {
                sb.append(text);
            }
            return sb.toString();
        }
        return null;
    }

    public void close() throws IOException {
        if (inputStream != null){
            inputStream.close();
        }
        if (reader != null){
            reader.close();
        }
    }
}